//
//  ForcePasswordModificationController.m
//  NtoccShipper
//
//  Created by 张天兴 on 2025/6/27.
//  Copyright © 2025 zhuchongjin. All rights reserved.
//

#import "ForcePasswordModificationController.h"
#import "LogInVC.h"

@interface ForcePasswordModificationController ()<UITextFieldDelegate>

@end

@implementation ForcePasswordModificationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.startTopHeight.constant =  kNavBarHeight + 10;
    self.bottomHeight.constant = BR_BOTTOM_MARGIN;
    [self setTitleName:@"新密码设置"];
    [self setBackLeftButton:@""];
    self.nextButton.layer.masksToBounds = YES;
    self.nextButton.layer.cornerRadius = 10;
    self.passwordTextField.secureTextEntry = YES;
    self.passwordTextField.tag = 101;
    self.NewPasswordTextField.tag = 102;
    self.agenTextField.tag = 103;
    self.passwordTextField.delegate = self;
    self.NewPasswordTextField.delegate = self;
    self.agenTextField.delegate = self;
    self.agenTextField.secureTextEntry = YES;
    self.NewPasswordTextField.secureTextEntry = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)newPasswordButtonAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        [self.NewLookButton setImage:[UIImage imageNamed:@"icon_close_eye"] forState:(UIControlStateNormal)];
        self.NewPasswordTextField.secureTextEntry = NO;
    }else{
        [self.NewLookButton setImage:[UIImage imageNamed:@"icon_open_eye"] forState:(UIControlStateNormal)];
        self.NewPasswordTextField.secureTextEntry = YES;
    }
}
- (IBAction)agenButtonAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        [self.agenLookImagButton setImage:[UIImage imageNamed:@"icon_close_eye"] forState:(UIControlStateNormal)];
        self.agenTextField.secureTextEntry = NO;
    }else{
        [self.agenLookImagButton setImage:[UIImage imageNamed:@"icon_open_eye"] forState:(UIControlStateNormal)];
        self.agenTextField.secureTextEntry = YES;
    }
}
- (IBAction)orderPassWordButtonAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        [self.noLookImagButton setImage:[UIImage imageNamed:@"icon_close_eye"] forState:(UIControlStateNormal)];
        self.passwordTextField.secureTextEntry = NO;
    }else{
        [self.noLookImagButton setImage:[UIImage imageNamed:@"icon_open_eye"] forState:(UIControlStateNormal)];
        self.passwordTextField.secureTextEntry = YES;
    }
}

- (IBAction)nextButtonAction:(UIButton *)sender {
    if (IsStrEmpty(self.passwordTextField.text)) {
        [CJAlert showStringDismissWithDelay:@"旧密码不能为空"];
    }else if (IsStrEmpty(self.NewPasswordTextField.text)){
        [CJAlert showStringDismissWithDelay:@"新密码不能为空"];
    }else if (IsStrEmpty(self.agenTextField.text)){
        [CJAlert showStringDismissWithDelay:@"确认密码不能为空"];
    }else if (![self.NewPasswordTextField.text isEqualToString:self.agenTextField.text]){
        [CJAlert showStringDismissWithDelay:@"两次新密码不一致请重新输入"];
    }else{
            
        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/party/updatePass",CJBaseUrl);
        NSMutableDictionary *dicPra = [NSMutableDictionary dictionary];
        if ([self.isChangePassword isEqualToString:@"1"]) {
            [dicPra setValue:[CJControl base64:self.NewPasswordTextField.text] forKey:@"newPwd"];
            [dicPra setValue:[CJControl base64:self.passwordTextField.text] forKey:@"oldPwd"];
        }else{
            [dicPra setValue:[CJControl base64:self.NewPasswordTextField.text] forKey:@"newPwd"];
            [dicPra setValue:[CJControl base64:self.passwordTextField.text] forKey:@"oldPwd"];
            [dicPra setValue:self.phoneStr forKey:@"phone"];
            [dicPra setValue:self.codeStr forKey:@"verifyCode"];
        }

        WS(ws);
        [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strUrl parameters:dicPra status:OperationTip success:^(id  _Nonnull responseObject) {
            [CJControl clearAllUserDefaultsData];
            LogInVC *vc = [[LogInVC alloc] init];
            vc.isRegsiter = YES;
            [ws pushVc:vc];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.NewPasswordTextField) {
            NSMutableString *str = [NSMutableString stringWithString:textField.text];
            [str replaceCharactersInRange:range withString:string];
            NSLog(@"%@",str);
        bool dxbool = [self containsUpperCaseLetters:str];
        bool ddBool = [self containsUpperCaseLettersD:str];
        bool numBool = [self containsDigit:str];
        bool tsBool = [self containsSpecialCharacter:str];
        if (dxbool == YES && ddBool == YES) {
            self.firstLabel.textColor = [UIColor colorWithRed:82/255.0 green:179/255.0 blue:170/255.0 alpha:1];
            self.firstImagView.image = [UIImage imageNamed:@"loginPasswordD"];
        }else{
            self.firstLabel.textColor = [UIColor colorWithRed:237/255.0 green:107/255.0 blue:134/255.0 alpha:1];
            self.firstImagView.image = [UIImage imageNamed:@"loginPasswordC"];
        }
        if (numBool) {
            self.twoLabel.textColor = [UIColor colorWithRed:82/255.0 green:179/255.0 blue:170/255.0 alpha:1];
            self.twoImagView.image = [UIImage imageNamed:@"loginPasswordD"];
        }else{
            self.twoLabel.textColor = [UIColor colorWithRed:237/255.0 green:107/255.0 blue:134/255.0 alpha:1];
            self.twoImagView.image = [UIImage imageNamed:@"loginPasswordC"];
        }
        if (str.length >= 8) {
            self.threeLabel.textColor = [UIColor colorWithRed:82/255.0 green:179/255.0 blue:170/255.0 alpha:1];
            self.threeImagView.image = [UIImage imageNamed:@"loginPasswordD"];
        }else{
            self.threeLabel.textColor = [UIColor colorWithRed:237/255.0 green:107/255.0 blue:134/255.0 alpha:1];
            self.threeImagView.image = [UIImage imageNamed:@"loginPasswordC"];
        }
        if (tsBool) {
            self.forLabel.textColor = [UIColor colorWithRed:82/255.0 green:179/255.0 blue:170/255.0 alpha:1];
            self.forImagView.image = [UIImage imageNamed:@"loginPasswordD"];
        }else{
            self.forLabel.textColor = [UIColor colorWithRed:237/255.0 green:107/255.0 blue:134/255.0 alpha:1];
            self.forImagView.image = [UIImage imageNamed:@"loginPasswordC"];
        }
        if (str.length == 0) {
            self.firstLabel.textColor = [UIColor colorWithRed:199/255.0 green:205/255.0 blue:213/255.0 alpha:1];
            self.firstImagView.image = [UIImage imageNamed:@"loginPasswordM"];
            self.twoLabel.textColor = [UIColor colorWithRed:199/255.0 green:205/255.0 blue:213/255.0 alpha:1];
            self.twoImagView.image = [UIImage imageNamed:@"loginPasswordM"];
            self.threeLabel.textColor = [UIColor colorWithRed:199/255.0 green:205/255.0 blue:213/255.0 alpha:1];
            self.threeImagView.image = [UIImage imageNamed:@"loginPasswordM"];
            self.forLabel.textColor = [UIColor colorWithRed:199/255.0 green:205/255.0 blue:213/255.0 alpha:1];
            self.forImagView.image = [UIImage imageNamed:@"loginPasswordM"];
        }
        }
    
    return YES;
}
//是否包含小写
- (BOOL)containsUpperCaseLetters:(NSString *)str{
    for (int i = 0; i < str.length; i++) {
        unichar c = [str characterAtIndex:i];
        if((c > 64)&&(c < 91)) {
            return YES;
        }
    }
    return NO;
}
//是否包含大写
- (BOOL)containsUpperCaseLettersD:(NSString *)str{
    for (int i = 0; i < str.length; i++) {
           unichar c = [str characterAtIndex:i];
           if((c > 96)&&(c < 123)) {
               return YES;
           }
       }
    return NO;
}
//是否包含数字
- (BOOL)containsDigit:(NSString *)str{
    for (int i = 0; i < str.length; i++) {
        unichar c = [str characterAtIndex:i];
        if((c > 47)&&(c < 58)) {
            return YES;
        }
    }
    return NO;
}
//是否包含特殊字符
- (BOOL)containsSpecialCharacter:(NSString *)str{
    
    NSString *specialCharacters = @"~`!@#$%^&*()_+-=[]|{};':\",.<>?/";//规定的特殊字符，可以自己随意添加
    
    for (int i = 0; i < str.length; i++) {
        NSString *subStr = [str substringWithRange:NSMakeRange(i, 1)];
        if([specialCharacters containsString:subStr]) {
            return YES;
        }
    }
    return NO;
}
@end
